define(function(require){
    var main = require('/static/main.js');
    var m = new main();
    var user = {
        isActive : false,
        activeindex :0,
        userList :[],
        table : '',
        init:function(){
            this.bindEvent();
            this.getList();
            this.getLevelList();
        },
        getList:function(){
            //获取管理员列表
            user.table = $("#datatable").dataTable({
                "ordering" : false,
                "serverSide": true,
                "bSort": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    var param = {};
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/jurisdiction/get-admin-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            user.userList = result.data;
                            callback(returnData);
                        }
                    });
                },
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": false,
                "autoWidth": false,

                "aoColumns": [
                    {"mDataProp": "Nickname"},
                    {"mDataProp": "Phone"},
                    {"mDataProp": "LevelName"},
                    {"mDataProp": "IsLock",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            if(oData.IsLock){
                                $(nTd).html('锁定');
                            }else{
                                $(nTd).html('正常');
                            }
                        }
                    },
                    {
                        "mDataProp": "MytipId",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<div class="option_btn"><span class="LockAdmin"   data-mytipid="'+oData.MytipId+'" data-type="'+!oData.IsLock+'">'+(!oData.IsLock ?  '锁定':'正常')+'</span>\
                               <span class="ResetPassword"  data-mytipid="'+oData.MytipId+'">重置密码</span>\
                               <span class="EditAdmin" data-toggle="modal" data-target="#EditUser">编辑</span>\
                               <span class="DelUser" data-mytipid="'+oData.MytipId+'">删除</span></div>')

                        }
                    },
                ],
                "fnCreatedRow": function (nRow, aData, iDataIndex) {
                },
                "fnInitComplete": function (oSettings, json) {
                }
            });
        },
        getLevelList:function(){
            $.ajax({
                type: "POST",
                url:"/jurisdiction/get-level-list",
                dataType: "json",
                success: function (ret) {
                 var str  = ' <option value="-1">请选择</option>';
                    $.each(ret.data,function(k,v){
                        str += ' <option value="'+ v.MytipLevelId+'">'+ v.LevelName+'</option>';
                    })
                    $(".level-list").html(str);
                }
            });
        },
        bindEvent:function(){
            //新增
            var addaction = false;
            $(document).on('click','#AddUser .btn-sure',function(){
                if(addaction){
                    return false;
                }
                var JsonData ={
                    NickName : $("#AddUser .NickName").val().trim(),
                    Phone :  $("#AddUser .Phone").val().trim(),
                    Password :  $("#AddUser .Password").val().trim(),
                    rePassword :  $("#AddUser .rePassword").val().trim(),
                    MytipLevel :  parseInt($("#AddUser .level-list").val())
                }
                if(JsonData.NickName.length < 1){
                    user.showmessage($("#AddUser .error"),true,'请输入管理员名称');
                    return false;
                }
                if(JsonData.Phone.length < 1){
                    user.showmessage($("#AddUser .error"),true,'请输入管理员账号');
                    return false;
                }
                if(!m.IsPhone(JsonData.Phone)){
                    user.showmessage($("#AddUser .error"),true,'账号格式不正确');
                    return false;
                }

                if(JsonData.Password.length < 1){
                    user.showmessage($("#AddUser .error"),true,'请输入密码');
                    return false;
                }

                if(JsonData.rePassword.length < 1){
                    user.showmessage($("#AddUser .error"),true,'请输入重复密码');
                    return false;
                }

                if(JsonData.Password != JsonData.rePassword){
                    user.showmessage($("#AddUser .error"),true,'两次密码输入不一致');
                    return false;
                }
                if(JsonData.MytipLevel == -1){
                    user.showmessage($("#AddUser .error"),true,'请选择等级');
                    return false;
                }
                addaction = true;
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: '/jurisdiction/mytip-info-create',
                    data:JsonData,
                    success: function (ret) {
                        addaction = false;
                        if(ret.IsBizSuccess){
                            $("#AddUser").modal('hide');
                            user.resetFrom($("#AddUser"));
                            var table = $('#datatable').DataTable();
                            table.ajax.reload();
                            layer.msg('成功添加账户！');
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });
            });



            //锁定
            $(document).on('click','.LockAdmin',function(){
                var _this = $(this);
                layer.confirm('确定此操作吗吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    var JsonData = {
                        MytipId:_this.data("mytipid"),
                        IsLock:_this.data("type")
                    }
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: '/jurisdiction/mytip-setstate',
                        data:JsonData,
                        success: function (ret) {
                            if(ret.IsBizSuccess){
                                var table = $('#datatable').DataTable();
                                table.ajax.reload();
                                layer.msg('操作成功！');
                            }else{
                                layer.msg(ret.ErrMsg);
                            }
                        }
                    });
                }, function(){
                });
            });
            //重置密码
            $(document).on('click','.ResetPassword',function(){
                var MytipId = $(this).data("mytipid");
                layer.confirm('确定此操作吗吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: '/jurisdiction/admin-reset-pwd',
                        data:{MytipId:MytipId},
                        success: function (ret) {
                            if(ret.IsBizSuccess){
                                layer.msg('操作成功！');
                            }else{
                                layer.msg(ret.ErrMsg);
                            }
                        }
                    });
                }, function(){
                });
            });
            //编辑
            $(document).on('click','.EditAdmin',function(){
               var index = $(this).closest("tr").index();
                user.activeindex = index;
                var data = user.userList[index];
                $("#EditUser .NickName").val(data.Nickname);
                $("#EditUser .Phone").val(data.Phone);
                $("#EditUser .MytipLevel").val(data.MytipLevel);
                user.showmessage($("#EditUser .error"),false);
            });
            //编辑提交
            var editaction = false;
            $(document).on('click','#EditUser .btn-sure',function(){
                if(editaction){
                    return false;
                }
                var data = user.userList[user.activeindex];
                var JsonData ={
                    AlterMytipId : data.MytipId,
                    NickName : $("#EditUser .NickName").val().trim(),
                    NewPhone :  $("#EditUser .Phone").val().trim(),
                    MytipLevel:parseInt($("#EditUser .MytipLevel").val().trim()),
                    OldPhone :  data.Phone
                }

               if(JsonData.NickName.length < 1){
                   user.showmessage($("#EditUser .error"),true,'请输入管理员名称');
                    return false;
               }
                if(JsonData.NewPhone.length < 1){
                    user.showmessage($("#EditUser .error"),true,'请输入管理员账号');
                    return false;
                }
                if(!m.IsPhone(JsonData.NewPhone)){
                    user.showmessage($("#EditUser .error"),true,'账号格式不正确');
                    return false;
                }
                if(JsonData.MytipLevel == -1){
                    user.showmessage($("#EditUser .error"),true,'请选择等级');
                    return false;
                }
                editaction = true;
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: '/jurisdiction/alert-mytip-info',
                    data:JsonData,
                    success: function (ret) {
                        editaction = false;
                        $("#EditUser").modal('hide');
                        user.resetFrom($("#EditUser"));
                        if(ret.IsBizSuccess){
                            var table = $('#datatable').DataTable();
                            table.ajax.reload();
                            layer.msg('修改成功！');
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });
            });


            //删除
            $(document).on('click','.DelUser',function(){
                var MytipId = $(this).data("mytipid");
                layer.confirm('确定此操作吗吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    var MytipIdList = [MytipId] ;
                    console.log(MytipIdList);
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: '/jurisdiction/mytip-delete',
                        data:{MytipId:MytipIdList},
                        success: function (ret) {
                            if(ret.IsBizSuccess){
                                var table = $('#datatable').DataTable();
                                table.ajax.reload();
                                layer.msg('成功删除！');
                            }else{
                                layer.msg(ret.ErrMsg);
                            }
                        }
                    });
                }, function(){
                });
            });

        },
        showmessage:function(_obj,isshow,str){
            if(isshow){
                _obj.html(str).show();
            }else{
                _obj.html('').hide();
            }
        },
        //重置表单
        resetFrom:  function (_obj){
            _obj.find("form").each(function (index) {
                _obj.find("form")[index].reset();
            });
            user.showmessage(_obj.find(".error"),false);
        }
    }
    user.init();
});
